var layer;
var $;
var element;
var form;
var laydate;
var table;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    //执行一个laydate实例
    laydate.render({
        elem: '#createDate', //指定元素
        type: 'date',
        trigger: 'click' //时间时间为点击事件
    });
    load();
    selIndexUserTab();
})
/**
 *  点击事件  刷新  返回 搜索
 * */
function load(){
    //刷新页面按钮
    $(".tit_btn").bind('click',function(){
        location.reload();
    })
    //返回按钮
    $(".tit_btn2").bind('click',function(){
        window.history.go(-1);  //返回上一页
    })
    //检索按钮
    $("#indexBtn").bind('click',function(){
        var obj = '';
        var deptName = $("input[name=deptName]").val();
        var trueName = $("input[name=trueName]").val();
        var createDate = $("input[name=createDate]").val();
        var deptStatus = $("select[name=deptStatus]").val();
        if(!deptName){
            obj+="&deptName="+'';
        }else{
            obj+="&deptName="+deptName;
        }
        if(!trueName){
            obj+="&trueName="+'';
        }else{
            obj+="&trueName="+trueName;
        }
        if(!createDate){
            obj+="&createDate="+'';
        }else{
            obj+="&createDate="+createDate;
        }
        if(!deptStatus){
            obj+="&deptStatus="+0;
        }else{
            obj+="&deptStatus="+deptStatus;
        }
        selIndexUserTab(obj);
        layer.msg("搜索成功", {icon: 1});
    })
    //add_btn  添加部门按钮
    $("#add_btn").bind('click',function(){
        addDeptInfo();
    })
}
/**
 *  部门数据管理列表
 */
function selIndexUserTab(vals){
    $.ajax({
        url:"/personnel/selDeptList",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(item) {
            userTab(item.obj);
        }
    })
}
function userTab(item){
    table.render({
        elem:"#userListTab",//容器
        title:"部门数据管理列表",
        limit:10,//分页显示条数
        width:1225,
        data:item,
        toolbar:true,
        page:{//自定义分页
            layout:['count','prev','page','next','limit','skip','refresh'],
            limits:[3,6,9],//分页选项
            first:"首页",
            last:"尾页",
        },
        cols:[[//定义列
            {type:'checkbox',align:'center',width:60},//复选框靠左
            {title:'序号',align:'center',width:100,templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"deptName",title:"部门名称",align:'center',width:140},
            {field:"deptDescirbe",title:"部门描述",align:'center',width:220},
            {field:"trueName",title:"创建人",align:'center',width:200},
            {field:"createDate",title:"创建时间",align:'center',width:220},
            {field:"deptStatus",title:"状态",align:'center',width:125,templet:function(data){
                    if(data.deptStatus==1){
                        return"<a style='display:inline-block;' onclick='updDeptStatus(\""+data.deptId+"\",\""+data.deptName+"\",\""+data.deptStatus+"\")'><input type=\"checkbox\"  checked lay-skin=\"switch\"  lay-filter='filter' lay-text='启用|停用'>"
                    }else if(data.deptStatus==2){
                        return"<a style='display:inline-block;' onclick='updDeptStatus(\""+data.deptId+"\",\""+data.deptName+"\",\""+data.deptStatus+"\")'><input type=\"checkbox\" lay-skin=\"switch\"  lay-filter='filter' lay-text='启用|停用'>"
                    }
                }},
            {field:"",title:"操作",align:'center',width:150,templet:function(data){
                    return "<a style='color:#009688;cursor:pointer;' onclick='updDeptInfo(\""+data.deptId+"\")'>编辑</a> " +
                        "<a style='color:#009688;cursor:pointer;' onclick='delDeptInfo(\""+data.deptId+"\")'>删除</a>" ;
                }}
        ]]
    });
}
/**
 *  更改为停用状态
 */
function updDeptStatus(deptId,deptName,deptStatus){
    if(deptStatus==1){
        layer.confirm("您确定要停用`"+deptName+"`吗?", {
            cancel:function(index, layero){
                layer.msg("停用取消",{icon:2});
                selIndexUserTab();
            }
        },function (rel) {
            var status = 2;
            $.post("/personnel/updDeptStatus", {deptId:deptId,deptStatus:status}, function (res) {
                if(res.obj){
                    var tablename = "部门数据管理列表";
                    var type = 4;
                    var remark = deptId+"";
                    addOperLog(tablename,type,remark);
                    layer.msg("停用成功",{icon:1});
                    selIndexUserTab();
                }else{
                    layer.msg("停用失败",{icon:2});
                    selIndexUserTab();
                }
            });
        },function (res) {
            layer.msg("停用取消",{icon:2});
            selIndexUserTab();
        });
    }else if(deptStatus==2){
        layer.confirm("您确定要启用`"+deptName+"`吗?",{
            cancel:function(index, layero){
                layer.msg("启用取消",{icon:2});
                selIndexUserTab();
            }
        },function (rel) {
            var status = 1;
            $.post("/personnel/updDeptStatus", {deptId:deptId,deptStatus:status}, function (res) {
                if(res.obj){
                    var tablename = "部门数据管理列表";
                    var type = 4;
                    var remark = deptId+"";
                    addOperLog(tablename,type,remark);
                    layer.msg("启用成功",{icon:1});
                    selIndexUserTab();
                }else{
                    layer.msg("启用失败",{icon:2});
                    selIndexUserTab();
                }
            });
        },function (res) {
            layer.msg("启用取消",{icon:2});
            selIndexUserTab();
        });
    }
}
/**
 *  添加部门addDeptInfo
 * */
function addDeptInfo(){
    layer.open({
        type:2,//弹出层的类型
        title:"编译部门信息",
        area:['550px','400px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/PerDept_layer_addDeptInfo.html",
        btn:['添加','取消'],
        success:function(layero,index){
            var body = layero.find('iframe').contents();
            var now = new Date();
            var year = now.getFullYear(); //得到年份
            var month = now.getMonth()+1;//得到月份
            var date = now.getDate();//得到日期
            var hour= now.getHours();//得到小时数
            var minute= now.getMinutes();//得到分钟数
            var second= now.getSeconds();//得到秒数
            var nowTime = year+"年"+month+"月"+date+"日 "+hour+"时"+minute+"分"+second+"秒";
            body.find("input[name=createDate]").val(nowTime);
        },
        yes:function(index,layero){
            var body = layero.find('iframe').contents();
            var deptName = body.find("input[name=deptName]").val();
            var deptStatus = body.find("select[name=deptStatus]").val();
            var deptDescirbe = body.find("textarea[name=deptDescirbe]").val();
            var create=localStorage.getItem("user");
            create=JSON.parse(create);//将获取json字符串转json对象
            var createId = create.userId;
            var val = '';
            if(!deptName){
                layer.msg("部门名称不能为空",{icon:5});
            }else{
                val+="&deptName="+deptName;
                if(!deptDescirbe){
                    layer.msg("部门描述不能为空",{icon:5});
                }else{
                    val+="&deptDescirbe="+deptDescirbe;
                    if(!createId){
                        val+="&createId="+0;
                    }else{
                        val+="&createId="+createId;
                    }
                    if(!deptStatus){
                        val+="&deptStatus="+0;
                    }else{
                        val+="&deptStatus="+deptStatus;
                    }
                    layer.confirm("您确定要添加'"+deptName+"'吗?", function (rel) {
                        $.post("/personnel/addDeptInfo", val, function (res) {
                            if(res.obj){
                                var tablename = "部门数据管理列表";
                                var type = 2;
                                var remark = 0+"";
                                addOperLog(tablename,type,remark);
                                layer.msg("添加成功",{icon:1});
                                selIndexUserTab();
                                layer.closeAll('iframe');
                            }else{
                                layer.msg("添加失败",{icon:2});
                                selIndexUserTab();
                                layer.closeAll('iframe');
                            }
                        });
                    });
                }
            }
        },btn2:function(index,layero){

        }
    })
}
/**
 *  编辑部门信息
 */
function updDeptInfo(deptId){
    layer.open({
        type:2,//弹出层的类型
        title:"编译部门信息",
        area:['550px','400px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/PerDept_layer_updDeptInfo.html",
        success:function(layero,index){
            //查找当前引入的iframe的内容
            $.post("/personnel/selDeptList", {deptId:deptId}, function (res) {
                var obj = layero.find('iframe').contents();
                obj.find("input[name=deptName]").val(res.obj[0].deptName);
                obj.find("input[name=trueName]").val(res.obj[0].trueName);
                obj.find("input[name=createDate]").val(res.obj[0].createDate);
                obj.find("textarea[name=deptDescirbe]").val(res.obj[0].deptDescirbe);
            });
        },
        btn:['编辑','取消'],
        yes:function(index,layero){
            var body = layero.find('iframe').contents();
            var deptName = body.find("input[name=deptName]").val();
            var deptDescirbe = body.find("textarea[name=deptDescirbe]").val();
            var create=localStorage.getItem("user");
            create=JSON.parse(create);//将获取json字符串转json对象
            var updateId = create.userId;
            var val = '';
            if(!deptName){
                val+="&deptName="+'';
            }else{
                val+="&deptName="+deptName;
            }
            if(!deptDescirbe){
                val+="&deptDescirbe="+'';
            }else{
                val+="&deptDescirbe="+deptDescirbe;
            }
            if(!updateId){
                val+="&updateId="+0;
            }else{
                val+="&updateId="+updateId;
            }
            if(!deptId){
                val+="&deptId="+0;
            }else{
                val+="&deptId="+deptId;
            }
            layer.confirm("您确定要修改此部门吗?", function (rel) {
                $.post("/personnel/updDeptInfo", val, function (res) {
                    if(res.obj){
                        var tablename = "部门数据管理列表";
                        var type = 4;
                        var remark = deptId+"";
                        addOperLog(tablename,type,remark);
                        layer.msg("编辑成功",{icon:1});
                        selIndexUserTab();
                        layer.closeAll('iframe');
                    }else{
                        layer.msg("编辑失败",{icon:2});
                        selIndexUserTab();
                        layer.closeAll('iframe');
                    }
                });
            });
        },btn2:function(index,layero){

        }
    })
}
/**
 *  删除部门信息
 */
function delDeptInfo(deptId){
    layer.confirm("您确定要删除此部门吗?", function (rel) {
        $.post("/personnel/delDeptInfo", {deptId:deptId}, function (res) {
            if(res.obj){
                var tablename = "部门数据管理列表";
                var type = 3;
                var remark = deptId+"";
                addOperLog(tablename,type,remark);
                layer.msg("删除成功",{icon:1});
                selIndexUserTab();
                layer.closeAll('iframe');
            }else{
                layer.msg("删除失败",{icon:2});
                selIndexUserTab();
                layer.closeAll('iframe');
            }
        });
    });
}
/**
 *  添加后台操作日志
 * */
function addOperLog(tablename,type,remark){
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var mid = create.userId;
    form+="&mid="+mid;
    form+="&tablename="+tablename;
    form+="&type="+type;
    form+="&remark="+remark;
    $.post("/personnel/addOperationLog", form, function (res) {
        form="";
    });
}